<?php

namespace App\Http\Controllers\Develop;

use Illuminate\Http\Request;

use App\Models\User\Log;

use App\Http\Controllers\Controller;

class LogController extends Controller
{
    public function index()
    {
        return view ('develop/LogList');
    }

    //日志列表
    public function search(Request $request)
    {
        $where = [];

        $tableName = $request->input('tableName');
        $tableRemark = $request->input('tableRemark');
        $days = $request->input('days');

        if (!empty($tableName)) {
            $where[] = ['tableName',$tableName];
        }
        if (!empty($tableRemark)) {
            $where[] = ['tableRemark',$tableRemark];
        }
        if (!empty($days)) {
            $where[] = ['days',$days];
        }

        $log_data = Log::where($where)
            ->orderBy($request->input('sort'), $request->input('order'))
            ->paginate($request->input('limit'))
            ->toArray();

        //返回数组
        $return = [
            'total' => $log_data['total'],
            'rows' => []
        ];

        if ( $log_data['data'] ) {

            foreach( $log_data['data'] as $row ){

                $operation = '';
                $operation .='<a href="javascript: void(0);" onclick="Log.del(' . $row['manageId'] . ')">删除</a>&nbsp;&nbsp;
                            <a href="javascript: void(0);" onclick="Log.edit(' . $row['manageId'] . ')">修改</a>&nbsp;&nbsp;';

                $return['rows'][] = [
                    'operate' => $operation,
                    'tableName' => $row['tableName'],
                    'tableRemark' => $row['tableRemark'],
                    'days' => $row['days']
                ];

            }

        }

        return response()->json($return);

    }

    //获取单条流水号信息
    public function get($manageId)
    {

        $log_one = Log::find($manageId);

        return response()->json(['code'=>200, 'message'=>'ok', 'data'=>$log_one]);

    }

    //保存流水号信息
    public function store(Request $request)
    {

        $manageId = $request->input('manageId');
        $tableName = $request->input('tableName');
        $tableRemark = $request->input('tableRemark');
        $days = $request->input('days');

        if ( empty($tableName) ) {
            return response()->json(array(
                'code' => 100001,
                'message' => '日志表名不能为空'
            ));
        }

        if ( !isset($tableRemark) ) {
            return response()->json(array(
                'code' => 100002,
                'message' => '日志表备注不能为空'
            ));
        }

        if ( empty($days) ) {
            return response()->json(array(
                'code' => 100003,
                'message' => '清除天数不能为空'
            ));
        }
        $log = Log::find($manageId);

        if ( !$log ) {
            $log = new Log();
        }

        $log->tableName = $tableName;
        $log->tableRemark = $tableRemark;
        $log->days = $days;

        $log->save();

        return response()->json(['code' => 200, 'message' => '保存成功']);
    }

    //删除流水号信息
    public function delete($manageId)
    {

        $log = Log::find($manageId);

        if ( $log ) {
            $log->delete();
        }

        return response()->json(['code' => 200, 'message' => '删除成功']);

    }
}
